package com.dog.basics.sys.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dog.basics.bean.dto.admin.sys.SysConfigAddDto;
import com.dog.basics.bean.dto.admin.sys.SysConfigDto;
import com.dog.basics.bean.dto.admin.sys.SysConfigEditDto;
import com.dog.basics.bean.model.sys.SysConfig;
import com.dog.basics.bean.vo.admin.sys.SysConfigInfoVo;
import com.dog.basics.bean.vo.admin.sys.SysConfigPageVo;
import com.dog.basics.common.annotation.SysLog;
import com.dog.basics.common.constant.SwaggerConstants;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.common.util.R;
import com.dog.basics.dao.sys.SysConfigMapper;
import com.dog.basics.security.annotation.Inner;
import com.dog.basics.service.sys.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;


/**
 * 系统配置信息
 *
 * @author dog
 */
@RestController
@RequestMapping("/sys/config")
@Api(value = SwaggerConstants.SYSTEM+"系统配置信息模块", tags = SwaggerConstants.SYSTEM+"系统配置信息模块")
@AllArgsConstructor
@ApiIgnore
public class SysConfigController {
    private final SysConfigMapper sysConfigMapper;
    private final ISysConfigService sysConfigService;

    /**
     * 所有配置列表
     */
    @ApiOperation(value = "分页配置列表", notes = "sys:config:page")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('sys:config:page')")
    @Inner
    public R<IPage<SysConfigPageVo>> page(PageParam page, SysConfigDto config) {
        IPage<SysConfigPageVo> sysConfigs = sysConfigService.getSysConfigPage(page, config);
        return R.ok(sysConfigs);
    }


    /**
     * 配置信息
     */
    @ApiOperation(value = "配置详情", notes = "sys:config:info")
    @ApiImplicitParam(name = "id", value = "配置信息ID", required = true, paramType = "Long")
    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('sys:config:info')")
    public R<SysConfigInfoVo> info(@PathVariable("id") Long id) {
        SysConfig config = sysConfigService.getById(id);
        SysConfigInfoVo sysConfigInfoVo = new SysConfigInfoVo();
        BeanUtil.copyProperties(config, sysConfigInfoVo);
        return R.ok(sysConfigInfoVo);
    }

    /**
     * 保存配置
     */
    @SysLog("保存配置")
    @PostMapping("/save")
    @ApiOperation(value = "保存配置", notes = "sys:config:save")
    @PreAuthorize("@pms.hasPermission('sys:config:save')")
    @Inner
    public R<Boolean> save(@RequestBody @Valid SysConfigAddDto config) {
        return R.ok(sysConfigService.saveSysConfigDto(config));
    }

    /**
     * 修改配置
     */
    @SysLog("修改配置")
    @ApiOperation(value = "修改配置", notes = "sys:config:update")
    @PutMapping("/update")
    @PreAuthorize("@pms.hasPermission('sys:config:update')")
    @Inner
    public R<Boolean> update(@Valid @RequestBody SysConfigEditDto config) {
        return R.ok(sysConfigService.updateSysConfigDto(config));
    }

    /**
     * 删除配置
     */
    @SysLog("删除配置")
    @ApiOperation(value = "删除配置", notes = "sys:config:delete")
    @ApiImplicitParam(value = "配信信息Id", name = "ids", dataType = "List<Long>", required = true)
    @DeleteMapping("/delete")
    @PreAuthorize("@pms.hasPermission('sys:config:delete')")
    @Inner
    public R<Boolean> delete(@RequestBody List<Long> ids) {
        sysConfigService.removeByIds(ids);
        return R.ok();
    }


}
